import entity_ from "./entity.js";
import mapper_ from "./mapper.js";
import service_ from "./service.js";
import controller_ from "./controller.js";
import mybatis_ from "./mybatis.js";
import json_ from "./json.js";
import form from "./form.js"
import rule from "./rule.js";

/**
 * 字符转义
 *
 * @param unsafe {string} -
 * @returns {string} -
 */
const escape = (unsafe) => {
    return unsafe
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
}

/**
 * 字符反转义
 *
 * @param safe {string} - 已转义的字符串
 * @returns {string} - 反转义后的原始字符串
 */
const unescape = (safe) => {
    return safe
        .replace(/&amp;/g, "&")
        .replace(/&lt;/g, "<")
        .replace(/&gt;/g, ">")
        .replace(/&quot;/g, '"')
        .replace(/&#039;/g, "'");
};

/**
 * 代码扩展参数
 */
const attach = {
    "daoPackage": "cn.seaboot.admin.reverse.dao",
    "beanPackage": "cn.seaboot.admin.reverse.bean",
    "servicePackage": "cn.seaboot.admin.reverse.service",
    "controllerPackage": "cn.seaboot.admin.reverse.web"
}

/**
 * 代码生成器
 *
 * 通过 javascript 引擎生成代码，简单地通过字符串拼接，生成所需的字符串。
 *
 * 相比于模版引擎，javascript 可以兼容任何复杂的业务，可以进行非常精细的格式微调。
 *
 * 业务中，如果需要很复杂的逻辑处理，推荐使用这种方式。
 *
 * 注意：这些代码会放到 java 中执行，受到版本限制，只能支持 ES5 以下的语法。
 */
export default {
    entity: (map, es = false) => {
        map.attach = attach;
        const code = entity_(map);
        return es ? escape(code) : code;
    },
    mapper: (map, es = false) => {
        map.attach = attach;
        const code = mapper_(map);
        return es ? escape(code) : code;
    },
    mybatis: (map, es = false) => {
        map.attach = attach;
        const code = mybatis_(map);
        return es ? escape(code) : code;
    },
    service: (map, es = false) => {
        map.attach = attach;
        const code = service_(map);
        return es ? escape(code) : code;
    },
    controller: (map, es = false) => {
        map.attach = attach;
        const code = controller_(map);
        return es ? escape(code) : code;
    },
    json: (map, es = false) => {
        map.attach = attach;
        const code = json_(map);
        return es ? escape(code) : code;
    },
    form: (map, es = false) => {
        map.attach = attach;
        const code = form(map);
        return es ? escape(code) : code;
    },
    rule: (map, es = false) => {
        map.attach = attach;
        const code = rule(map);
        return es ? escape(code) : code;
    },
}



